/*************************************************************************
 Copyright (C) Unpublished Unis Software, Inc. All rights reserved.
 Unis Software, Inc., Confidential and Proprietary.

 This software is subject to copyright protection
 under the laws of the Public of China and other countries.

 Unless otherwise explicitly stated, this software is provided
 by Unis "AS IS".
 *************************************************************************/
package com.unis.web.confsummary;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.beanutils.BeanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.unis.common.ui.jqGrid.GridUIMapper;
import com.unis.common.util.WebUtil;
import com.unis.model.BaseModel;
import com.unis.model.attach.AttachLink;
import com.unis.model.attach.Attachment;
import com.unis.model.confsummary.ConfSummary;
import com.unis.service.attach.AttachLinkService;
import com.unis.service.attach.AttachmentService;
import com.unis.service.confsummary.ConfSummaryService;

/**
 * @author Alucard (email:357375084@qq.com)
 * @since 2013-03-25
 */
@Controller
@RequestMapping("/confsummary")
public class ConfSummaryController {

    private ConfSummaryService confsummaryService;
    private AttachLinkService attachlinkService;
    private AttachmentService attachmentService;

    /****************************************
     * Insert a instace of ConfSummary
     ****************************************/
    @RequestMapping("/insert")
    @ResponseBody
    public Object insertConfSummary(@RequestBody ConfSummary confsummary) throws Exception {
        confsummary.setStatmentId("ConfSummary.insert");
        String status = "failure";
        confsummaryService.insert(confsummary);
        status = "success";
        return status;
    }

    /****************************************
     * Update a instace of ConfSummary
     ****************************************/
    @RequestMapping("/update")
    @ResponseBody
    public Object update(@RequestBody ConfSummary confsummary) throws Exception {
        confsummary.setStatmentId("ConfSummary.update");
        String status = "failure";
        confsummaryService.update(confsummary);
        status = "success";
        return status;
    }

    /****************************************
     * Delete a instace of ConfSummary
     ****************************************/
    @RequestMapping("/delete")
    @ResponseBody
    public Object delete(@RequestBody ConfSummary confsummary) throws Exception {
        confsummary.setStatmentId("ConfSummary.delete");
        String status = "failure";
        confsummaryService.batchDelete(confsummary);
        status = "success";
        return status;
    }

    /****************************************
     * Query instaces of ConfSummary
     ****************************************/
    @RequestMapping("/queryAsList")
    @ResponseBody
    public Object queryAsList(HttpServletRequest request) throws Exception {
        Map<String, Object> paramMap = WebUtil.getParamsMapFromRequest(request);
        ConfSummary confsummary = new ConfSummary();
        confsummary.setStatmentId("ConfSummary.queryAsList");
        BeanUtils.populate(confsummary, paramMap);
        Map<String, Object> pageMap = GridUIMapper.process(paramMap);
        BeanUtils.populate(confsummary, pageMap);
        List<BaseModel> confsummaryList = confsummaryService.queryAsList(confsummary);
        Map<String, Object> renderMap = GridUIMapper.render(confsummary, confsummaryList);
        return renderMap;
    }

    /****************************************
     * 查询附件
     ****************************************/
    @RequestMapping("/selectAttachment/{confsummaryid}")
    @ResponseBody
    public List<?> getAttachment(@PathVariable String confsummaryid) throws Exception {
        List<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
        AttachLink al = new AttachLink();
        al.setEntityid(confsummaryid);
        al.setStatmentId("AttachLink.queryAsList");
        al.setPaging(false);
        List<BaseModel> attachlinklist = attachlinkService.queryAsList(al);
        for (BaseModel basemodel : attachlinklist) {
            AttachLink attachlink = (AttachLink) basemodel;
            Attachment attachment = new Attachment();
            attachment.setFileid(attachlink.getFileid());
            attachment.setStatmentId("Attachment.queryAsObject");
            attachment=(Attachment) attachmentService.queryAsObject(attachment);
            HashMap<String, String> map = new HashMap<String, String>();
            map.put("fileid", attachlink.getFileid());
            map.put("filename", attachment.getFilename());
            list.add(map);
        }
        return list;
    }
    
    /****************************************
     * Query a instaces of ConfSummary
     ****************************************/
    @RequestMapping("/queryAsObject")
    @ResponseBody
    public BaseModel queryAsObject(@RequestBody ConfSummary confsummary) throws Exception {
        confsummary.setStatmentId("ConfSummary.queryAsObject");
        return confsummaryService.queryAsObject(confsummary);
    }

    @RequestMapping("/page")
    public String forword() {
        return "confsummary/confsummaryList";
    }

    @Resource
    public void setConfSummaryService(ConfSummaryService confsummaryService) {
        this.confsummaryService = confsummaryService;
    }
    
    @Resource
    public void setAttachmentService(AttachmentService attachmentService) {
        this.attachmentService = attachmentService;
    }
    
    @Resource
    public void setAttachlinkService(AttachLinkService attachlinkService) {
        this.attachlinkService = attachlinkService;
    }
    
    
}
